package com.mdd.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.product.entity.ProductProperty;
import com.mdd.product.mapper.ProductPropertyMapper;
import com.mdd.product.service.IProductPropertyService;
import com.mdd.product.service.IProductPropertyValueService;
import com.mdd.product.validate.ProductPropertyCreateValidate;
import com.mdd.product.validate.ProductPropertyUpdateValidate;
import com.mdd.product.vo.ProductPropertyDetailVo;
import com.mdd.product.vo.ProductPropertyValueDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 商品属性项实现类
 * @author LikeAdmin
 */
@Service
public class ProductPropertyServiceImpl implements IProductPropertyService {

    @Resource
    ProductPropertyMapper productPropertyMapper;

    @Resource
    IProductPropertyValueService iProductPropertyValueService;

    /**
     * 商品属性项列表
     *
     * @author LikeAdmin
     * @return PageResult<OmsProductPropertyListedVo>
     */
    @Override
    public List<ProductPropertyDetailVo> list(Long spuId) {
        LambdaQueryWrapper<ProductProperty> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductProperty::getSpuId,spuId);

        List<ProductProperty> data = productPropertyMapper.selectList(queryWrapper);

        List<ProductPropertyDetailVo> list = new LinkedList<>();
        for(ProductProperty item : data) {
            ProductPropertyDetailVo vo = new ProductPropertyDetailVo();
            BeanUtils.copyProperties(item, vo);

            List<ProductPropertyValueDetailVo> productPropertyValueDetailVos = iProductPropertyValueService.list(vo.getId());
            vo.setPropertyValues(productPropertyValueDetailVos);
            list.add(vo);
        }

        return list;
    }

    /**
     * 商品属性项详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OmsProductProperty
     */
    @Override
    public ProductPropertyDetailVo detail(Long id) {
        ProductProperty model = productPropertyMapper.selectById(id);

        Assert.notNull(model, "商品属性项数据不存在");

        ProductPropertyDetailVo vo = new ProductPropertyDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 商品属性项新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public Long add(ProductPropertyCreateValidate createValidate) {
        ProductProperty model = new ProductProperty();
        model.setSpuId(createValidate.getSpuId());
        model.setName(createValidate.getName());
        model.setRemark(createValidate.getRemark());
        model.setStatus(0);
        productPropertyMapper.insert(model);
        return model.getId();
    }

    /**
     * 商品属性项编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(ProductPropertyUpdateValidate updateValidate) {
        ProductProperty model = productPropertyMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "商品属性项数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setRemark(updateValidate.getRemark());
        productPropertyMapper.updateById(model);
    }

    /**
     * 商品属性项删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        ProductProperty model = productPropertyMapper.selectById(id);

        Assert.notNull(model, "商品属性项数据不存在!");

        productPropertyMapper.deleteById(id);
    }

    @Override
    public void delIds(List ids) {
        productPropertyMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Object> listDeletedIds(Long spuId, Set<Long> ids) {
        QueryWrapper<ProductProperty> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        if(!ids.isEmpty()) {
            queryWrapper.notIn("id",ids);
        }
        queryWrapper.eq("spu_id", spuId);
        return   productPropertyMapper.selectObjs(queryWrapper);
    }

}
